package com.mk.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.mk.base.exception.ApplicationException;
import com.mk.base.exception.StatusCode;
import com.mk.base.service.impl.BaseService;
import com.mk.common.BeanCopier;
import com.mk.common.CollectionUtil;
import com.mk.common.Config;
import com.mk.mapper.ResourceMapper;
import com.mk.model.entity.Resource;
import com.mk.model.entity.RoleResource;
import com.mk.model.request.ResourceRequest;
import com.mk.service.IResourceService;
import com.mk.service.IRoleResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * @Author: yq
 * @Description:
 * @Date: Created in 4:14 PM 3/13/18
 */
@Service
public class ResourceService extends BaseService<ResourceMapper,Resource> implements IResourceService {

    @Autowired
    private IRoleResourceService roleResourceService;

    @Override
    @Transactional
    public Resource save(ResourceRequest request) {
        if(request == null)
            throw new ApplicationException(StatusCode.BAD_REQUEST.getCode(),"请传入需要添加的资源信息");
        if(StringUtils.isEmpty(request.getName()) || StringUtils.isEmpty(request.getCode())
                || StringUtils.isEmpty(request.getType()) || request.getParentId() == null)
            throw new ApplicationException(StatusCode.BAD_REQUEST.getCode(),"添加资源时请传入资源名称、代码、类型以及父节点不可为空");
        Resource existByCode = get(request.getCode());
        if(existByCode != null)
            throw new ApplicationException(StatusCode.UNIQUE_KE.getCode(),"资源代码不可重复");
        Resource resource = BeanCopier.copy(request,Resource.class);
        Date nowDate = new Date();
        resource.setCreateTime(nowDate);
        resource.setUpdateTime(nowDate);
        if(super.insert(resource))
            return resource;
        else
            throw new ApplicationException(StatusCode.CONFLICT.getCode(), "添加资源失败");
    }

    @Override
    @Transactional
    public Boolean update(ResourceRequest request) {
        if(request == null || request.getId() == null)
            throw new ApplicationException(StatusCode.BAD_REQUEST.getCode(),"请选择需要编辑的资源信息");
        Resource exist = get(request.getId());
        if(exist == null)
            throw new ApplicationException(StatusCode.CONFLICT.getCode(),"编辑的资源不存在");
        if(!StringUtils.isEmpty(request.getCode())){
            exist = get(request.getCode());
            if(exist != null && !exist.getId().equals(request.getId()))
                throw new ApplicationException(StatusCode.CONFLICT.getCode(),"编辑后的代码已被使用");
        }
        exist = BeanCopier.copy(request,Resource.class);
        exist.setUpdateTime(new Date());
        return super.updateById(exist);
    }

    @Override
    @Transactional
    public Boolean delete(Long id) {
        if(id == null)
            throw new ApplicationException(StatusCode.BAD_REQUEST.getCode(),"请选择需要删除的资源信息");
        Resource existById = get(id);
        if(existById == null)
            throw new ApplicationException(StatusCode.CONFLICT.getCode(),"删除的资源信息不存在");
        List<RoleResource> roleResources = roleResourceService.getByResourceId(id);
        if(roleResources != null && roleResources.size() > 0)
            throw new ApplicationException(StatusCode.CONFLICT.getCode(),"该资源正在使用，无法删除");
        existById.setUpdateTime(new Date());
        existById.setIsDeleted(Config.ABLE_CONFIG.UNABLE);
        return super.updateById(existById);
    }

    @Override
    public Resource get(Long id) {
        return super.selectById(id);
    }

    @Override
    public Resource get(String code) {
        return super.selectOne(new EntityWrapper<Resource>()
                .eq("code",code)
                .eq(Config.ABLE_CONFIG.ABLE_COLUMN,Config.ABLE_CONFIG.ABLE));
    }

    @Override
    public List<Resource> getByUsername(String username) {
        return baseMapper.findByUsername(username,Config.ABLE_CONFIG.ABLE);
    }

    @Override
    public Set<String> getCodesByUsername(String username) {
        List<String> codes = baseMapper.findCodesByUsername(username,Config.ABLE_CONFIG.ABLE);
        return CollectionUtil.listToSet(codes);
    }

    @Override
    public List<Resource> getList() {
        return super.selectList(new EntityWrapper<Resource>().eq(Config.ABLE_CONFIG.ABLE_COLUMN,Config.ABLE_CONFIG.ABLE));
    }

    @Override
    public List<Resource> getResourcesByRoleId(Long roleId) {
        return baseMapper.findByRoleId(roleId,Config.ABLE_CONFIG.ABLE);
    }

    @Override
    public List<Resource> getResourcesByRoleCode(String roleCode) {
        return baseMapper.findByRoleCode(roleCode,Config.ABLE_CONFIG.ABLE);
    }

    @Override
    public List<Resource> getResources(Long parentId, String type) {
        return baseMapper.findByParentAndType(parentId,type,Config.ABLE_CONFIG.ABLE);
    }
}
